package com.wanxi.product.util;

import com.wanxi.product.core_sevice.SkuService;
import com.wanxi.product.core_sevice.SpuService;
import com.wanxi.product.service.api.dto.SkuDto;
import com.wanxi.product.service.api.dto.SpuDto;
import com.wanxi.product.service.api.dto.TypeDto;
import com.wanxi.product.service.api.util.TypeEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 权限校验工具类
 */
@Component
public class Check {
    @Resource
    private SpuService spuService;

    @Resource
    private SkuService skuService;

    /**
     * 查找该SPU是否在该商家管理下
     *
     * @param spuId
     * @param enterpriseId
     * @return
     */
    public boolean checkEnterpriseSpu(Long spuId, Long enterpriseId) {
        try {
            SpuDto spuDto = spuService.findSpuBySpuId(spuId);
            return spuDto.getEnterpriseId().equals(enterpriseId);
        }catch (NullPointerException e){
            return false;
        }
    }

    /**
     * 查找该SKU是否在该商家管理下
     *
     * @param skuId
     * @param enterpriseId
     * @return
     */
    public boolean checkEnterpriseSku(Long skuId, Long enterpriseId) {
        try {
            SkuDto skuDto = skuService.findSkuBySkuId(skuId);
            SpuDto spuDto = spuService.findSpuBySpuId(skuDto.getSpuId());
            return spuDto.getEnterpriseId().equals(enterpriseId);
        }catch (NullPointerException e){
            return false;
        }
    }

    /**
     * 检查分类级别
     *
     * @param typeId
     * @return
     */
    public TypeEnum checkTypeLevel(Long typeId) {
        try{
            TypeDto typeDto = spuService.findTypeByTypeId(typeId);
            if (typeDto.getParentId() != 0) {
                TypeDto secondParentTypeDto = spuService.findTypeByTypeId(typeDto.getParentId());
                if (secondParentTypeDto.getParentId() != 0) {
                    return TypeEnum.三级分类;
                } else {
                    return TypeEnum.二级分类;
                }
            }
            return TypeEnum.一级分类;
        }catch (NullPointerException e){
            return TypeEnum.三级分类;
        }
    }
}
